package main

import (
	"bytes"
	"fmt"
	"net/http"
	"os"
	"time"

	// "github.com/Unknwon/martini"

	"github.com/bitly/go-simplejson"
	"github.com/go-martini/martini"
)

func normal_handler(res http.ResponseWriter, req *http.Request, c martini.Context) martini.Handler {
	var buffer bytes.Buffer
	_, err := buffer.ReadFrom(req.Body)
	// if err != nil {
	// 	ren.JSON(http.StatusBadRequest, JsonStruct{Error: err.Error()})
	// 	return
	// }

	// Seems ok with buffer
	var body string
	body = string(buffer.Bytes())
	// fmt.Println(body)
	fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
	fmt.Fprintf(os.Stdout, "%s\n", body)
	js, err := simplejson.NewJson([]byte(`{
		"result":true
	}`))
	if err != nil {
		panic("json format error")
	}
	jsfmt, _ := js.Encode()
	return string(jsfmt)

	// body, _ := ctx.Req.Body().String()
	// fmt.Fprintf(os.Stdout, "%s\n", body)
	// return "{\r\n\"result\": true\r\n}"
}

func POST_func(m *martini.ClassicMartini) {
	m.Post("/scan/port", normal_handler)
	m.Post("/scan/order/done", normal_handler)
	m.Post("/scan/port/port", normal_handler)
	m.Post("/scan/panel/change", normal_handler)
	m.Post("/scan/port/change", normal_handler)
	m.Post("/scan/order/run", normal_handler)
	m.Post("/scan/order/done", normal_handler)
	m.Post("/scan/alarm/delete/12345678abcd", normal_handler)
}

func GET_func(m *martini.ClassicMartini) {
	m.Get("/scan/order/list", func(res http.ResponseWriter, req *http.Request, c martini.Context) martini.Handler {
		var buffer bytes.Buffer
		_, err := buffer.ReadFrom(req.Body)
		// if err != nil {
		// 	ren.JSON(http.StatusBadRequest, JsonStruct{Error: err.Error()})
		// 	return
		// }

		// Seems ok with buffer
		var body string
		body = string(buffer.Bytes())
		fmt.Fprintf(os.Stdout, "/scan/order/list: %s\n", body)

		js, err := simplejson.NewJson([]byte(`{
			"pages": 10,
			"data":[
			{
				"id":1,
				"sn":"12345678abcd",
				"act":"add",
				"region":"f-3,room-A315,port-3FD31",
				"self": true
			},
			{
				"id":2,
				"sn":"12345678abce",
				"act":"delete",
				"region":"f-3,room-A315,port-3FD31",
				"self": true
			},
			{
				"id":3,
				"sn":"12345698abcf",
				"act":"change",
				"region":"f-3,room-A315,port-3FD31",
				"self": true
			},
			{
				"id":4,
				"sn":"12345678abcf",
				"act":"add",
				"region":"f-3,room-A315,port-3FD31",
				"self": true
			},
			{
				"id":5,
				"sn":"12345998abce",
				"act":"delete",
				"region":"f-3,room-A315,port-3FD31",
				"self": false
			}]
		}`))
		if err != nil {
			panic("json format error")
		}
		jsfmt, _ := js.Encode()
		return string(jsfmt)
	})

	m.Get("/scan/order/detail/1", func(res http.ResponseWriter, req *http.Request, c martini.Context) martini.Handler {
		var buffer bytes.Buffer
		_, err := buffer.ReadFrom(req.Body)
		// if err != nil {
		// 	ren.JSON(http.StatusBadRequest, JsonStruct{Error: err.Error()})
		// 	return
		// }

		// Seems ok with buffer
		var body string
		body = string(buffer.Bytes())
		fmt.Fprintf(os.Stdout, "/scan/order/detail: %s\n", body)
		js, err := simplejson.NewJson([]byte(`{
			"id":1,
			"sn":"12345678abcd",
			"status":false,
			"type":"one",
			"act":"add",
			"region":"f-3,room-A315,port-3FD31",
			"pre":1,
			"next":2,
			"remain": 5,
			"data":[
				{
					"region":"cab-01,ep-01,port-01",
					"act":"in",
					"note":"0000000000000001"
				}
			]
		}`))
		if err != nil {
			panic("json format error")
		}
		jsfmt, _ := js.Encode()
		return string(jsfmt)
	})

	m.Get("/scan/order/detail/2", func(res http.ResponseWriter, req *http.Request, c martini.Context) martini.Handler {
		var buffer bytes.Buffer
		_, err := buffer.ReadFrom(req.Body)
		// if err != nil {
		// 	ren.JSON(http.StatusBadRequest, JsonStruct{Error: err.Error()})
		// 	return
		// }

		// Seems ok with buffer
		var body string
		body = string(buffer.Bytes())
		fmt.Fprintf(os.Stdout, "/scan/order/detail: %s\n", body)
		js, err := simplejson.NewJson([]byte(`{
			"id":3,
			"sn":"12345698abcf",
			"status":false,
			"type":"one",
			"act":"delete",
			"region":"f-3,room-A315,port-3FD31",
			"pre":1,
			"next":2,
			"remain": 5,
			"data":[
				{
					"region":"cab-01,ep-01,port-01",
					"act":"out",
					"note":"0000000000000001"
				}
			]
		}`))
		if err != nil {
			panic("json format error")
		}
		jsfmt, _ := js.Encode()
		return string(jsfmt)
	})

	m.Get("/scan/order/detail/3", func(res http.ResponseWriter, req *http.Request, c martini.Context) martini.Handler {
		var buffer bytes.Buffer
		_, err := buffer.ReadFrom(req.Body)
		// if err != nil {
		// 	ren.JSON(http.StatusBadRequest, JsonStruct{Error: err.Error()})
		// 	return
		// }

		// Seems ok with buffer
		var body string
		body = string(buffer.Bytes())
		fmt.Fprintf(os.Stdout, "/scan/order/detail: %s\n", body)
		js, err := simplejson.NewJson([]byte(`{
			"id":3,
			"sn":"12345698abcf",
			"status":false,
			"type":"one",
			"act":"change",
			"region":"f-3,room-A315,port-3FD31",
			"pre":1,
			"next":2,
			"remain": 5,
			"data":[
				{
					"region":"cab-01,ep-01,port-01",
					"act":"out",
					"note":"0000000000000001"
				},
				{
					"region":"cab-01,ep-01,port-01",
					"act":"in",
					"note":"0000000000000002"
				}
			]
		}`))
		if err != nil {
			panic("json format error")
		}
		jsfmt, _ := js.Encode()
		return string(jsfmt)
	})

	m.Get("/scan/order/detail/4", func(res http.ResponseWriter, req *http.Request, c martini.Context) martini.Handler {
		var buffer bytes.Buffer
		_, err := buffer.ReadFrom(req.Body)
		// if err != nil {
		// 	ren.JSON(http.StatusBadRequest, JsonStruct{Error: err.Error()})
		// 	return
		// }

		// Seems ok with buffer
		var body string
		body = string(buffer.Bytes())
		fmt.Fprintf(os.Stdout, "/scan/order/detail: %s\n", body)
		js, err := simplejson.NewJson([]byte(`{
			"id":4,
			"sn":"12345678abcf",
			"status":false,
			"type":"one",
			"act":"add",
			"region":"f-3,room-A315,port-3FD31",
			"pre":1,
			"next":2,
			"remain": 5,
			"data":[
				{
					"region":"cab-01,ep-01,port-01",
					"act":"in",
					"note":"0000000000000002"
				}
			]
		}`))
		if err != nil {
			panic("json format error")
		}
		jsfmt, _ := js.Encode()
		return string(jsfmt)
	})

	m.Get("/scan/alarm/list", func(res http.ResponseWriter, req *http.Request, c martini.Context) martini.Handler {
		var buffer bytes.Buffer
		_, err := buffer.ReadFrom(req.Body)
		// if err != nil {
		// 	ren.JSON(http.StatusBadRequest, JsonStruct{Error: err.Error()})
		// 	return
		// }

		// Seems ok with buffer
		var body string
		body = string(buffer.Bytes())
		fmt.Fprintf(os.Stdout, "/scan/alarm/list: %s\n", body)

		js, err := simplejson.NewJson([]byte(`{
			"pages": 5,
			"data":[
			{
				"id":11,
				"region": "ep-001-3",
				"alarm":"out",
				"time":"10:00:34/20150608",
				"self": true
			},
			{
				"id":12,
				"region": "ep-002-3",
				"alarm":"in",
				"time":"10:00:34/20150609",
				"self": true
			},
			{
				"id":13,
				"region": "ep-003-5",
				"alarm":"out",
				"time":"10:00:34/20150610",
				"self": false
			}]
		}`))
		if err != nil {
			panic("json format error")
		}
		jsfmt, _ := js.Encode()
		return string(jsfmt)
	})

	m.Get("/scan/alarm/detail/11", func(res http.ResponseWriter, req *http.Request, c martini.Context) martini.Handler {
		var buffer bytes.Buffer
		_, err := buffer.ReadFrom(req.Body)
		// if err != nil {
		// 	ren.JSON(http.StatusBadRequest, JsonStruct{Error: err.Error()})
		// 	return
		// }

		// Seems ok with buffer
		var body string
		body = string(buffer.Bytes())
		fmt.Fprintf(os.Stdout, "/scan/alarm/detail: %s\n", body)
		js, err := simplejson.NewJson([]byte(`{
			"id":11,
			"dev":"cab-01,ep-01",
			"port":3,
			"pre":11,
			"next":12,
			"remain": 3,
			"time":"10:00:34/20150608",
			"alarm":"out"
		}`))
		if err != nil {
			panic("json format error")
		}
		jsfmt, _ := js.Encode()
		return string(jsfmt)
	})

	m.Get("/scan/alarm/detail/12", func(res http.ResponseWriter, req *http.Request, c martini.Context) martini.Handler {
		var buffer bytes.Buffer
		_, err := buffer.ReadFrom(req.Body)
		// if err != nil {
		// 	ren.JSON(http.StatusBadRequest, JsonStruct{Error: err.Error()})
		// 	return
		// }

		// Seems ok with buffer
		var body string
		body = string(buffer.Bytes())
		fmt.Fprintf(os.Stdout, "/scan/alarm/detail: %s\n", body)
		js, err := simplejson.NewJson([]byte(`{
			"id":12,
			"dev":"cab-01,ep-02",
			"port":3,
			"pre":11,
			"next":12,
			"remain": 3,
			"time":"10:00:34/20150609",
			"alarm":"in"
		}`))
		if err != nil {
			panic("json format error")
		}
		jsfmt, _ := js.Encode()
		return string(jsfmt)
	})

	m.Get("/scan/main", func(res http.ResponseWriter, req *http.Request, c martini.Context) martini.Handler {
		var buffer bytes.Buffer
		_, err := buffer.ReadFrom(req.Body)
		// if err != nil {
		// 	ren.JSON(http.StatusBadRequest, JsonStruct{Error: err.Error()})
		// 	return
		// }

		// Seems ok with buffer
		var body string
		body = string(buffer.Bytes())
		fmt.Fprintf(os.Stdout, "/scan/main: %s\n", body)
		js, err := simplejson.NewJson([]byte(`{
			"order":3,
			"alarm":10
		}`))
		if err != nil {
			panic("json format error")
		}
		jsfmt, _ := js.Encode()
		return string(jsfmt)
	})
}
